﻿using System.Collections.Generic;

namespace DotNetCRM.Core.ExtensionMethods
{
    public static class DictionaryExtensions
    {
        /// <summary>
        /// Removes all instances of <c>obj</c> from
        /// the dictionary
        /// </summary>
        /// <param name="parm"></param>
        /// <param name="obj"></param>
        public static void RemoveAllInstances<T, U>(this Dictionary<T, U> parm, U obj)
        {
            if (parm == null) return;

            List<T> toRemove = new List<T>();
            foreach (KeyValuePair<T, U> keyValue in parm)
            {
                if (!Equals(keyValue.Key, default(T)) && Equals(keyValue.Value, obj))
                {
                    toRemove.Add(keyValue.Key);
                }
            }
            foreach (T key in toRemove)
            {
                parm.Remove(key);
            }
        }

        public static bool ContainsKeyAndValue<T, U>(this Dictionary<T, U> parm, T key) where U : class
        {
            return parm != null && parm.ContainsKey(key) && parm[key] != null;
        }
    }
}